package org.example.managementsystem.controller;

import org.example.managementsystem.model.Provider;
import org.example.managementsystem.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供应商控制器
 */
@RestController
@RequestMapping("/api/providers")
@CrossOrigin(origins = "*")
public class ProviderController {
    
    private final ProviderService providerService;

    @Autowired
    public ProviderController(ProviderService providerService) {
        this.providerService = providerService;
    }

    /**
     * 获取所有供应商
     */
    @GetMapping
    public ResponseEntity<List<Provider>> getAllProviders() {
        List<Provider> providers = providerService.getAllProviders();
        return ResponseEntity.ok(providers);
    }

    /**
     * 根据名称获取供应商
     */
    @GetMapping("/{providerName}")
    public ResponseEntity<Provider> getProviderByName(@PathVariable String providerName) {
        Provider provider = providerService.getProviderByName(providerName);
        if (provider != null) {
            return ResponseEntity.ok(provider);
        }
        return ResponseEntity.notFound().build();
    }

    /**
     * 添加供应商
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> addProvider(@RequestBody Provider provider) {
        Provider savedProvider = providerService.addProvider(provider);
        Map<String, Object> response = new HashMap<>();
        response.put("providerName", savedProvider.getProviderName());
        response.put("message", "供应商添加成功");
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    /**
     * 更新供应商信息
     */
    @PutMapping("/{providerName}")
    public ResponseEntity<Map<String, String>> updateProvider(@PathVariable String providerName, @RequestBody Provider provider) {
        provider.setProviderName(providerName);
        boolean updated = providerService.updateProvider(provider);
        
        Map<String, String> response = new HashMap<>();
        if (updated) {
            response.put("message", "供应商信息更新成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "供应商信息更新失败，未找到指定名称的供应商");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 删除供应商
     */
    @DeleteMapping("/{providerName}")
    public ResponseEntity<Map<String, String>> deleteProvider(@PathVariable String providerName) {
        boolean deleted = providerService.deleteProvider(providerName);
        
        Map<String, String> response = new HashMap<>();
        if (deleted) {
            response.put("message", "供应商删除成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "供应商删除失败，未找到指定名称的供应商");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 搜索供应商
     */
    @GetMapping("/search")
    public ResponseEntity<List<Provider>> searchProviders(@RequestParam String keyword) {
        List<Provider> providers = providerService.searchProviders(keyword);
        return ResponseEntity.ok(providers);
    }

    /**
     * 按评级搜索供应商
     */
    @GetMapping("/search/rating")
    public ResponseEntity<List<Provider>> searchProvidersByRating(@RequestParam Integer rating) {
        List<Provider> providers = providerService.searchProvidersByRating(rating);
        return ResponseEntity.ok(providers);
    }
}
